﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ApplicationCore;
using Microsoft.ApplicationBlocks.Data;

namespace SD.LLBLGen.Pro.Plugins
{
    public partial class EntityVisualizerControl : Form, IPluginWindow
    {     

        private string _connectionString;
        private EntityVisualizer _plugin;
        private string _entityName;

        public EntityVisualizerControl()
        {
            InitializeComponent();
        }     

        public EntityVisualizerControl(string connectionString, EntityVisualizer plugin)
        {
            InitializeComponent();
            _connectionString = connectionString;
            _plugin = plugin;

            string sqlText = "select name from sysobjects where type='U'  ";
            DataTable table = SqlHelper.ExecuteDataset(_connectionString, CommandType.Text, sqlText).Tables[0];
            cmbTables.DataSource = table;
            cmbTables.DisplayMember = "name";
            cmbTables.ValueMember = "name";
            cmbTables.SelectedIndex = 0;
        }

        private void EntityVisualizerControl_Load(object sender, EventArgs e)
        {
            
        }

        private void LoadTable()
        {
            DataRowView row=(DataRowView)cmbTables.Items[cmbTables.SelectedIndex];
            if(row!=null)
            {
                _entityName=row["name"].ToString();
            }

            if (String.IsNullOrEmpty(_connectionString) || String.IsNullOrEmpty(_entityName))
                return;

            string sqlText = string.Format("SELECT *  FROM  {0}", _entityName);
            try
            {
                DataTable table = SqlHelper.ExecuteDataset(_connectionString, CommandType.Text, sqlText).Tables[0];
                grid.DataSource = table;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to connection to the database server");
            }
           
        }

        public Guid FormID
        {
            get
            {
                return new Guid("{08F8C6A0-30B8-479f-BFB0-95139E6C7B3A}");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        /// <summary>
        /// The ID of the plugin which opened the IPluginWindow instance.
        /// </summary>
        /// <value></value>
        public Guid PluginID
        {
            get
            {
                return _plugin.Describe().Id;
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        private void cmbTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTable();
        }     
    }
}
